<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="java.sql.*" import="Connect.*" %>
  
<%
	// Redirect to the home page if the user is not a manager or employee.
	if (session.getAttribute("user_type") == null) {
		response.sendRedirect("no_permission.jsp");
	} else if (!session.getAttribute("user_type").equals("0") && !session.getAttribute("user_type").equals("1")) {
		response.sendRedirect("no_permission.jsp");
	}
	String username = (String)session.getAttribute("logged_user");
%>
  
  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="style.css" media="screen" />
<title>Hammer Time Auctions</title>

</head>
<body>
	<div id="content">
		<%@include file="header.jsp"%>
		<h1>Record Sales</h1>
		<hr></hr>
		<table class="center data-table">
			<%

				// Getting Auction information
				java.sql.Connection conn = null;
				try {
					// Connect to the database.
					Class.forName(ConnectionInfo.myJDBCDriver()).newInstance();
					java.util.Properties sysprops = System.getProperties();
					sysprops.put("user", ConnectionInfo.myUserID());
					sysprops.put("password", ConnectionInfo.myPassword());
					conn = java.sql.DriverManager.getConnection(ConnectionInfo.myURL(), sysprops);
					java.sql.Statement statement = conn.createStatement();
	 				
	 				java.sql.ResultSet rs = statement.executeQuery("SELECT auctions.id, items.name, auctions.minimum_bid, auctions.bid_increment, auctions.reserve_price, auctions.open_date, auctions.end_date "
							+", auctions.owner, auctions.item "
							+ "FROM auctions JOIN items ON item=items.id WHERE auctions.monitor='" + username +"'" 
							+ " AND " + "auctions.end_date <= NOW() AND auctions.in_progress=0");
	 				java.sql.ResultSetMetaData columns = rs.getMetaData();
					out.println("<tr>");
					for (int i = 1; i <= columns.getColumnCount(); i++) {						
							out.println("<th>" + columns.getColumnName(i) + "</th>");							
					}
					out.println("<th> Close </th>");
					out.println("<th></th>");
					out.println("</tr>");					
					out.println("<tr>");		
					
	 				while(rs.next()){
	 					String id = rs.getString("id");
						String minimum_bid = rs.getString("minimum_bid");
						String bid_increment = rs.getString("bid_increment");
						String reserve_price = rs.getString("reserve_price");						
						java.sql.Statement statement1 = conn.createStatement();
						java.sql.ResultSet rs1 = statement1.executeQuery("SELECT * FROM bids WHERE auction_id='" + id
		 						+ "' ORDER BY auction_id DESC, bid_price DESC, bid_time ASC");
		 				String current_bid = "0.00";
		 			
		 				if (rs1.next()) {
		 					double highest_bid = Double.parseDouble(rs1.getString("bid_price"));
		 					String highest_bidder = rs1.getString("bidder_username");
		 					boolean next_bid = rs1.next();
		 					if (next_bid == false) {		 						
		 						current_bid = ""
		 								+ ConnectionInfo.twoSigFigs(Double.parseDouble(minimum_bid)
		 										+ Double.parseDouble(bid_increment));
		 					} else {
		 						double next_highest_bid = Double.parseDouble(rs1.getString("bid_price"));
		 						if (next_highest_bid < highest_bid) {
		 							highest_bid = next_highest_bid + Double.parseDouble(bid_increment);
		 						}
		 						highest_bid = ConnectionInfo.twoSigFigs(highest_bid);
		 						String highest_bid_string = "" + ConnectionInfo.twoSigFigs(highest_bid);
		 						current_bid = "" + ConnectionInfo.twoSigFigs(highest_bid + Double.parseDouble(bid_increment));
		 					}
		 				}						
						
						if (Double.parseDouble(reserve_price)<=Double.parseDouble(current_bid)) {
							out.println("<tr>");							
							for (int i = 1; i <= columns.getColumnCount(); i++) {							
									out.println("<td align=center>" + rs.getString(columns.getColumnName(i)) + "</td>");							
							}						
							out.println("<td align=center><a href=\"item_update.jsp?aucowner=");%><%=rs.getString("owner") %><%out.println("&auctionid="); %><%=rs.getString("id") %>
							<%out.println("&itemid="); %><%=rs.getString("item") %>
							<%out.println("\" class=\"button\">Finish</a></td>");
							out.println("</tr>");
						}
	 				}
					
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					try {
						conn.close();
					} catch (Exception ee) {
					}
				}
			%>
		</table>
	</div>
	<hr></hr>
	<%@include file="footer.jsp"%>
</body>
</html> 